<template>
  <div class="app-container">
    <el-form
      class="searchform"
      :model="queryParams"
      ref="queryParams"
      size="small"
      label-width="120px"
    >
      <el-row>
        <el-col :span="5">
          <el-form-item label="检验方法编码">
            <el-input
              v-model="queryParams.number"
              placeholder="请输入"
              clearable
              @keyup.enter.native="getList()"
            />
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="检验方法名称">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入"
              clearable
              @keyup.enter.native="getList()"
            />
          </el-form-item>
        </el-col>
        <el-col :span="9">
          <el-form-item label-width="20px">
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="getList()"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="reset()"
              >重置</el-button
            >
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <div class="tableoperation">
      <el-button
        type="primary"
        icon="el-icon-plus"
        size="mini"
        plain
        @click="openDialog()"
        >添加检验方法</el-button
      >
    </div>

    <el-table v-loading="loading" height="400" :data="inspectionMethodList">
      <el-table-column label="检验方法编码" align="center" prop="number" />
      <el-table-column label="检验方法名称" align="center" prop="name" />
      <el-table-column label="检验方法描述" align="center" prop="description" />
      <el-table-column align="center" label="操作">
        <template slot-scope="{ row }">
          <el-button @click.native.prevent="openDialog(row)" type="text">
            编辑
          </el-button>
          <el-button
            @click.native.prevent="handleDelete(row)"
            type="text"
            style="color: #f32d2d"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加弹框 -->
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      v-dialogDrag="true"
    >
      <el-form
        :model="form"
        :rule="rules"
        ref="form"
        size="small"
        label-width="100px"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item label="检验方法编码" prop="number">
              <el-input v-model="form.number" placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="检验方法名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="方法描述">
              <el-input
                type="textarea"
                rows="3"
                maxlength="100"
                show-word-limit
                v-model="form.description"
                placeholder="请输入"
                clearable
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listUser,
  getUser,
  delUser,
  addUser,
  updateUser,
  resetUserPwd,
  changeUserStatus,
  deptTreeSelect,
} from "@/api/system/user";
import {
  inspectionMethodList,
  inspectionMethodExport,
  inspectionMethodDetail,
  inspectionMethodAdd,
  inspectionMethodEdit,
  inspectionMethodDelete,
} from "@/api/basicData/inspectionMethod";
import { getToken } from "@/utils/auth";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "inspectionMethod",
  dicts: ["sys_user_sex"],
  components: { Treeselect },
  data() {
    return {
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单校验
      rules: {
        number: [
          {
            required: true,
            message: "检验方法编码不能为空",
            trigger: "change",
          },
        ],
        name: [
          {
            required: true,
            message: "检验方法名称不能为空",
            trigger: "change",
          },
        ],
      },
      loading: false,
      // 检测方法列表
      inspectionMethodList: [],
      activeName: "0",
      total: 0,
      // 表单参数
      form: {},
      dialogVisible: false,
      status: "",
      title: "",
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 获取检测方法列表
    getList() {
      this.loading = true;
      inspectionMethodList(this.queryParams)
        .then((response) => {
          this.inspectionMethodList = response.rows;
          this.total = response.total;
          this.loading = false;
        })
        .catch((response) => {
          this.loading = false;
        });
    },
    // 打开对话框
    openDialog(row) {
      console.log("子row", row);
      this.resetForm("form");
      if (row) {
        this.status = "2";
        this.title = "编辑检测方法";
        inspectionMethodDetail(row.inspectionMethodId).then((response) => {
          this.form = response.data;
          this.dialogVisible = true;
        });
      } else {
        this.status = "1";
        this.title = "添加检测方法";
        this.form = {};
        this.dialogVisible = true;
      }
    },
    // 提交按钮
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.status == "1") {
            inspectionMethodAdd(this.form).then((response) => {
              this.$modal.msgSuccess("添加成功");
              this.dialogVisible = false;
              this.getList();
            });
          }
          if (this.status == "2") {
            inspectionMethodEdit(this.form).then((response) => {
              this.$modal.msgSuccess("编辑成功");
              this.dialogVisible = false;
              this.getList();
            });
          }
        }
      });
    },
    // 删除
    handleDelete(row) {
      this.$modal
        .confirm("是否确认删除数据？")
        .then(() => {
          this.loading = true;
          return inspectionMethodDelete(row.inspectionMethodId);
        })
        .then(() => {
          this.loading = false;
          this.$modal.msgSuccess("删除成功");
          this.getList();
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 表单重置
    reset() {
      (this.queryParams = {
        pageNum: 1,
        pageSize: 10,
      }),
        this.resetForm("queryParams");
      this.getList();
    },
  },
};
</script>

<style lang="scss" scoped>
.operation {
  margin-block: 30px;
}
::v-deep .el-textarea__inner {
  resize: none;
}
::v-deep .pagination-container {
  margin-top: 70px;
}
</style>
